﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.IO;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using EntLibContrib.Query.Configuration;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query.Commands
{
	/// <summary>
	/// Simple abstract class to hold the components of a file command
	/// </summary>
	[ConfigurationElementType(typeof(FileCustomCommandData))]
	public abstract class FileCommand : Command
	{
		#region Fields
		private string dataFile;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the data file.
		/// </summary>
		/// <value>The data file.</value>
		public string DataFile
		{
			get { return dataFile; }
			set { dataFile = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="FileCommand"/> class.
		/// </summary>
		protected FileCommand()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="FileCommand"/> class.
		/// </summary>
		/// <param name="dataFile">The data file.</param>
		/// <param name="parameters">The parameters.</param>
		protected FileCommand(string dataFile, ParameterDictionary parameters)
			: base(parameters)
		{
			ValidateAndStoreParameters(dataFile);
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Validates the and store parameters.
		/// </summary>
		/// <param name="filename">The filename.</param>
		private void ValidateAndStoreParameters(string filename)
		{
			// check for a null or empty path name
			if (string.IsNullOrEmpty(filename))
				throw new ArgumentNullException("filename", InternalResources.ExceptionFileCommandDataFileNullOrEmpty);

			// check that the file represents a valid path
			FileInfo fileInfo = new FileInfo(filename);
			if (!fileInfo.Exists)
				throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, InternalResources.ExceptionFileCommandDataFileInvalid, filename), filename);

			// store the file name
			this.dataFile = filename;
		}
		#endregion
	}
}
